from flask import Flask,render_template
from app.config import config
from app.extensions import congig_extensions
from app.views import config_blueprint


def create_app(config_name):
    # 实例化flask
    app = Flask(__name__)

    # 配置mysql数据库连接配置
    app.config.from_object(config.get(config_name) or config['default'])
    config[config_name].init_app(app)

    #初始化工具(bootstrap,sqlalchemy,migrate,moment)
    congig_extensions(app)

    # 错误界面配置
    config_errorhandler(app)

    # 蓝本配置
    config_blueprint(app)
    return app


def config_errorhandler(app):
    @app.errorhandler(404)
    def page_not_found(e):
        return render_template('errors/404.html',e=e)